package cn.easybuy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.ProductDao;
import cn.easybuy.entity.Product;

public class ProductDaoImpl extends BaseDao implements ProductDao{

	@Override
	public List<Product> getProductsByCategoryLevel3Id(int categoryLevel3Id) {
		String sql="select * from easybuy_product where categoryLevel3Id=?";
		ResultSet rs=this.exxcuteQuery(sql, categoryLevel3Id);
		List<Product> list=new ArrayList<Product>();
		try {
			while(rs.next()){
				Product product=new Product();
				product.setId(String.valueOf(rs.getObject(1)));
				product.setName(rs.getString(2));
				product.setDescription(rs.getString(3));
				product.setPrice(String.valueOf(rs.getObject(4)));
				product.setStock(String.valueOf(rs.getObject(5)));
				product.setCategoryLevel1Id(String.valueOf(rs.getObject(6)));
				product.setCategoryLevel2Id(String.valueOf(rs.getObject(7)));
				product.setCategoryLevel3Id(String.valueOf(rs.getObject(8)));
				product.setFileName(rs.getString(9));
				product.setIsDelete(String.valueOf(rs.getObject(10)));
				list.add(product);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public List<Product> geallProducts(int id) {
	String sql="select * from easybuy_product  where id=? ";
	ResultSet rs=this.exxcuteQuery(sql, id);
	List<Product> list=new ArrayList<Product>();
	try {
		while (rs.next()) {
			Product product=new Product();
			product.setId(String.valueOf(rs.getObject(1)));
			product.setName(rs.getString(2));
			product.setDescription(rs.getString(3));
			product.setPrice(String.valueOf(rs.getObject(4)));
			product.setStock(String.valueOf(rs.getObject(5)));
			product.setCategoryLevel1Id(String.valueOf(rs.getObject(6)));
			product.setCategoryLevel2Id(String.valueOf(rs.getObject(7)));
			product.setCategoryLevel3Id(String.valueOf(rs.getObject(8)));
			product.setFileName(rs.getString(9));
			product.setIsDelete(String.valueOf(rs.getObject(10)));
			list.add(product);
		} 
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		this.closeAll(connection, pstmt, rs);
	}
		return list;
	}

	@Override
	public List<Product> getProductCategories(int pageNo, int pageSize) {
		String sql="select * from easybuy_product  limit ?,? ";
		List<Product>  list =new ArrayList<>();
		ResultSet rs =this.exxcuteQuery(sql, (pageNo-1)*pageSize,pageSize);
		try {
			while (rs.next()) {
				Product product=new Product();
				product.setId(String.valueOf(rs.getObject(1)));
				product.setName(rs.getString(2));
				product.setDescription(rs.getString(3));
				product.setPrice(String.valueOf(rs.getObject(4)));
				product.setStock(String.valueOf(rs.getObject(5)));
				product.setCategoryLevel1Id(String.valueOf(rs.getObject(6)));
				product.setCategoryLevel2Id(String.valueOf(rs.getObject(7)));
				product.setCategoryLevel3Id(String.valueOf(rs.getObject(8)));
				product.setFileName(rs.getString(9));
				product.setIsDelete(String.valueOf(rs.getObject(10)));
				list.add(product);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount() {
		String sql ="select count(1) from easybuy_product_category";
		ResultSet rs =this.exxcuteQuery(sql);
		int count=-1;
		try {
			while (rs.next()) {
			count=	rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, pstmt, rs);
		}
		return count;
	}

	
}
